package com.xxl.shoppingapp.ui.home;

import android.os.Bundle;
import android.support.annotation.NonNull;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xxl.baselib.util.FastJsonTools;
import com.xxl.shoppingapp.base.BaseRecyclerViewAdapter;
import com.xxl.shoppingapp.base.BaseRecyclerViewFragment;
import com.xxl.shoppingapp.entity.BaseResponse;
import com.xxl.shoppingapp.entity.CommonProduct;
import com.xxl.shoppingapp.http.ResponseListener;
import com.xxl.shoppingapp.http.ShoppingRequest;
import com.xxl.shoppingapp.ui.ShopAppUIHelper;
import com.xxl.shoppingapp.ui.adapter.ProductListAdapter;


import java.util.ArrayList;
import java.util.List;

/**
 * @author xuelian.xiong 2018/4/17
 * @Package com.xxl.shoppingapp.ui.home
 * @Title: FreshFragment
 * @Description: (生鲜列表页)
 * Copyright (c) 传化公路港物流有限公司版权所有
 * Create DateTime: 2018/4/17
 */
public class ProductListFragment extends BaseRecyclerViewFragment {

    private ArrayList<CommonProduct>  orders = new ArrayList<>();;
    private static String TYPE = "ProductListFragment.type";
    private int type;
    static ProductListFragment instance;
    public synchronized static ProductListFragment getInstance(int type) {
        instance = new ProductListFragment();
        Bundle args = new Bundle();
        args.putInt(TYPE, type);
        instance.setArguments(args);
        return instance;
    }


    @Override
    protected void initAdapter() {
        adapter = new ProductListAdapter();
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle args = this.getArguments();
        hasBack(true);
        if (args != null) {
            type = args.getInt(TYPE);
        }

        switch (type) {
            case 0://天天热卖
                setTitle("天天热卖");
                break;
            case 1://必买清单
                setTitle("必买清单");
                break;
            case 2://有好货
                setTitle("有好货");
                break;
            case 3://秒杀专区
                setTitle("秒杀专区");
                break;
            case 4://秒杀专区
                setTitle("最新上架");
                break;


        }


        data();
        adapter.setItemClickListener(new BaseRecyclerViewAdapter.ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ShopAppUIHelper.navDetail(instance);
            }
        });
        getSwipeRefreshLayout().setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getSwipeRefreshLayout().finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });

        getSwipeRefreshLayout().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                adapter.addData(data());
                getSwipeRefreshLayout().finishLoadMore(2000);

            }
        });
    }

    private List<CommonProduct> data() {
//
//        for (int i = 0; i < 5; i++) {
//            CommonProduct order = new CommonProduct();
//
//            order.setName("豪尚豪牛排 10 片");
//
//            orders.add(i, order);
//        }

        showProgress("加载中。。。");
        ShoppingRequest.getProductListBySecType(type + "", new ResponseListener() {
            @Override
            public void onSuccess(String response) {
                orders.addAll(FastJsonTools.createJsonToListBean(response,CommonProduct.class));
                adapter.setData(orders);

             }

            @Override
            public void onFailed(String error) {

            }

            @Override
            public void onFinish() {
                hideProgress();
            }
        });
        return orders;
    }
}
